clear all
set more off
capture log close
set matsize 11000 

*---- Set Directory ----*
global rootDir = "/Users/AliceZhang/Dropbox/Research_Columbia/Renewables Voting (Urpelainen Zhang)/JOP/UZ_JOP2021_Replication"
global dataDir = "Data"
global resultDir = "Results"
global logDir = "Analysis/logSTATA"
global graphDir = "Results/Figures"

*---- Log File Path ----*
cd "$rootDir/$logDir"
log using 003_balanceTest, replace


*******************************************************************************
/*				     RENEWABLES_VOTING (URPELAINEN & ZHANG)	        		 */
*******************************************************************************

/* 

File Name:    	003_balanceTest.do

By:				Alice Tianbo Zhang (alice.tianbo.zhang@gmail.com)

Last Edited:  	10/11/2021

Purpose:		1. Summary statistics of votes wind panel and election district panel
				2. Exploratory data analysis of main variables
				3. Conduct balance test using ACS panel

Data Used:      votes_wind_panel.dta
				election_district_panel.dta	
				ACS_panel_balanceTest_recodeVar.dta
				
*/

** Install packages
*ssc install reghdfe
*ssc install tabout
*ssc install latab

*******************************************************************************
/*                 				 TABLE A1 	  							     */
*******************************************************************************
cd "$rootDir/$dataDir/Final"
use votes_wind_panel.dta, clear

eststo clear
eststo: estpost tabstat cum_count_turbine cum_capacity_turbine count_turbine capacity_turbine ///
				mean_wp std_wp min_wp median_wp max_wp ///
				pro_env anti_env ///
				incumbvotesmajorpercent demvotesmajorpercent repvotesmajorpercent, ///
				stat(mean sd min max count) col(stat)
local summary_var cum_count_turbine cum_capacity_turbine count_turbine capacity_turbine mean_wp std_wp min_wp median_wp max_wp pro_env anti_env incumbvotesmajorpercent demvotesmajorpercent repvotesmajorpercent

** Indent balance variables in table
foreach v of varlist `summary_var' {
	label variable `v' `"\hspace{0.2cm} `: variable label `v''"'
	}	

** Output table to LaTeX
cd "$rootDir/$resultDir/Tables"
esttab, cells("mean(fmt(%8.2f)) sd(fmt(%8.2f)) min(fmt(%8.0f)) max(fmt(%8.0f)) count(fmt(%8.0f))") coll(Mean SD Min Max Obs) ///
		varwidth(100) label wrap nostar unstack noobs nonote nomtitles nonumbers

esttab using TableA1.tex, booktabs replace ///
		refcat(cum_count_turbine "\emph{Wind Turbine Installation}" mean_wp "\emph{Zonal Wind Potential}" pro_env "\emph{Roll Call Vote Outcome }" incumbvotesmajorpercent "\emph{Election Outcome}" , nolabel) ///
		cells("mean(fmt(%8.2f)) sd(fmt(%8.2f)) min(fmt(%8.0f)) max(fmt(%8.0f)) count(fmt(%8.0f))") coll(Mean SD Min Max Obs) ///
		varwidth(100) label gaps nostar noobs nonote nomtitles nonumbers width(\hsize)

		
*******************************************************************************
/*                 				 TABLE A2 	  							     */
*******************************************************************************
cd "$rootDir/$dataDir/Final"
use election_district_panel.dta, clear

** Label variables and create turnout variable
gen turnout = (votes1 + votes2 + votes3 + votes4)/1000
label variable incumbvotesmajorpercent "Incumbent candidates vote share (\%)"
label variable demvotesmajorpercent "Democratic candidates vote share (\%)"
label variable repvotesmajorpercent "Republican candidates vote share (\%)"
label variable thirdvotestotalpercent "Third party candidates vote share (\%)" 
label variable turnout "Total number of votes (thousand)"
label variable demhouse "Number of Democrats in the House"
label variable rephouse "Number of Republicans in the House"
label variable indhouse "Number of Independents in the House"

tabstat turnout demvotesmajorpercent repvotesmajorpercent thirdvotestotalpercent ///
		incumbvotesmajorpercent demhouse rephouse indhouse, by(year) statistics(mean sd min max) nototal long

eststo clear 
foreach y in 2004 2006 2008 2010{

	estpost summarize turnout demvotesmajorpercent repvotesmajorpercent thirdvotestotalpercent ///
						incumbvotesmajorpercent demhouse rephouse indhouse if year == `y'
		
	est sto CY`y'
}

esttab CY2004 CY2006 CY2008 CY2010, main(mean %8.2f) aux(sd %8.2f) varwidth(30) ///
										   nostar nonote nonumber label mtitles("2004" "2006" "2008" "2010") width(\hsize)		
								   
** Output table to LaTeX
cd "$rootDir/$resultDir/Tables"
esttab CY2004 CY2006 CY2008 CY2010 using TableA2.tex, booktabs replace ///
										  main(mean %8.2f) aux(sd %8.2f) stats(N, labels("Districts") fmt(0)) ///
										  varwidth(30) gaps nostar nonote nonumber label ///
										  mtitles("2004" "2006" "2008" "2010") width(\hsize)		
		
		
*******************************************************************************
/*              				  FIGURE 2 	  							     */
*******************************************************************************
cd "$rootDir/$dataDir/Final"
use votes_wind_panel.dta, clear

*----------------------- Growth of wind power by year  ----------------------*
capture drop mean_count mean_capacity
bysort year: egen mean_count = mean(cum_count_turbine)
bysort year: egen mean_capacity = mean(cum_capacity_turbine)

graph twoway scatter mean_capa year, scheme(vg_s1c) graphregion(color(white)) ytitle("Mean Wind Capacity by District (MW)") yscale(range(0 120)) ylabel(0(20)120)

cd "$rootDir/$graphDir"
graph export fg2a.pdf, replace

graph twoway scatter mean_count year, scheme(vg_s1c) graphregion(color(white)) ytitle("Mean Wind Count by District") yscale(range(0 120)) ylabel(0(20)120)
cd "$rootDir/$graphDir"
graph export fg2b.pdf, replace

*******************************************************************************
/*                     				TABLE 1					   			     */
*******************************************************************************
cd "$rootDir/$dataDir/Final"
use votes_wind_panel.dta, clear

** Create dummy for treatment
egen dtreat = max(cum_count_turbine > 0 | cum_capacity_turbine > 0), by(state district)

** Summary of observations
unique year 
unique state
unique panelID
bysort dtreat: distinct panelID
bysort dtreat: count 


   
*******************************************************************************
/*                     				TABLE A3					   		     */
*******************************************************************************
cd "$rootDir/$dataDir/Final"
use ACS_panel_balanceTest_recodeVar.dta, clear

** Create list of local variables for balance test
local balance_var pop white foreign male old median_income average_income home_median non_poor edu_male edu_female prim_prod manu hours


*------------------------ Balance Test: Table -------------------------*
eststo clear
eststo: estpost sum `balance_var' if dtreat == 1
eststo: estpost sum `balance_var' if dtreat == 0
eststo: estpost sum `balance_var'

** Indent balance variables in table
foreach v of varlist `balance_var' {
	label variable `v' `"\hspace{0.2cm} `: variable label `v''"'
	}

** Output table to LaTex
cd "$rootDir/$resultDir/Tables"
esttab, main(mean) aux(sd) stats(N, labels("Observations") fmt(0)) ///
		label wrap nostar unstack noobs nonumbers nonote width(\hsize) ///
		mtitles("With Wind" "Without Wind" "Overall") 

esttab using TableA3.tex, booktabs replace ///
		refcat(pop "\emph{Demographics}" median_income "\emph{Income \& Poverty}" edu_male "\emph{Education}" prim_prod "\emph{Employment}", nolabel) ///
		main(mean) aux(sd) stats(N, labels("Observations") fmt(0)) ///
		label gaps nostar noobs nonumbers nonote width(\hsize) ///
		mtitles("With Wind" "Without Wind" "Overall") 

	
*******************************************************************************
/*                     				TABLES A4-A6				   		     */
*******************************************************************************
** Create interaction and fixed effects
gen t = year - 2005
gen inter = t * mean_wp

egen fixed = group(state year)
egen district_fixed = group(state district)

** Balance regressions
eststo clear
local balance_var pop white foreign male old median_income average_income home_value home_median non_poor hours edu_male edu_female prim_prod manu

foreach var in `balance_var'{
	eststo: reghdfe `var' inter, absorb(fixed district_fixed) vce(cluster district_fixed) 
	
}

** Demographics
esttab est1 est2 est3 est4 est5, ///
		b(%9.4f) stats(N N_clust, labels("Observations" "Districts") fmt(0 0)) ///
		eqlabels(none) noconstant se nonotes unstack legend star(* 0.10 ** 0.05 *** 0.01) ///
		varlabels(inter "Mean wind potential * time") varwidth(30) ///
		mtitles("Pop" "White" "Foreign" "Male" ">65")

esttab est1 est2 est3 est4 est5 using TableA4.tex, booktabs replace ///
		b(%9.4f) stats(N N_clust, labels("Observations" "Districts") fmt(0 0)) ///
		eqlabels(none) noconstant se nonotes unstack legend star(* 0.10 ** 0.05 *** 0.01) ///
		varlabels(inter "Mean wind potential * time") varwidth(30) ///
		mtitles("Pop" "White" "Foreign" "Male" "65+") width(\hsize)

** Income
esttab est6 est7 est9 est10, ///
		b(%9.4f) stats(N N_clust, labels("Observations" "Districts") fmt(0 0)) ///
		varlabels(inter "Mean wind potential * time") varwidth(30) ///
		eqlabels(none) noconstant se nonotes unstack legend star(* 0.10 ** 0.05 *** 0.01) ///
		mtitles("Med Inc" "PC Inc" "Med Rent" "Abv Poverty")

esttab est6 est7 est9 est10 using TableA5.tex, booktabs replace ///
		b(%9.4f) stats(N N_clust, labels("Observations" "Districts") fmt(0 0)) ///
		varlabels(inter "Mean wind potential * time") varwidth(30) ///
		eqlabels(none) noconstant se nonotes unstack legend star(* 0.10 ** 0.05 *** 0.01) ///
		mtitles("Med Inc" "PC Inc" "Med Rent" "Abv Poverty") width(\hsize)


** Education & Employment
esttab est11 est12 est13 est14 est15, ///
		b(%9.4f) stats(N N_clust, labels("Observations" "Districts") fmt(0 0)) ///
		varlabels(inter "Mean wind potential * time") varwidth(30) ///
		eqlabels(none) noconstant se nonotes unstack legend star(* 0.10 ** 0.05 *** 0.01) ///
		mtitles("Avg Hrs" "Male AS+" "Female AS+" "Empl Ag" "Empl Manuf")

esttab est11 est12 est13 est14 est15 using TableA6.tex, booktabs replace ///
		b(%9.4f) stats(N N_clust, labels("Observations" "Districts") fmt(0 0)) ///
		varlabels(inter "Mean wind potential * time") varwidth(30) ///
		eqlabels(none) noconstant se nonotes unstack legend star(* 0.10 ** 0.05 *** 0.01) ///
		mtitles("Avg Hrs" "Male AS+" "Female AS+" "Empl Ag" "Empl Manuf") width(\hsize) 

		
		
** Close log file
log close
cd "$rootDir/$logDir"
translate 003_balanceTest.smcl 003_balanceTest.log, replace
